package com.yunhe.rentcars.pro.service.finance.impl;

import com.yunhe.rentcars.pro.dao.finance.FinanceDao;
import com.yunhe.rentcars.pro.entity.finance.Finance;
import com.yunhe.rentcars.pro.entity.rent.Cars;
import com.yunhe.rentcars.pro.entity.system.Dept;
import com.yunhe.rentcars.pro.service.finance.FinanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/11/24 0024.
 */
@Service
public class FinanceServiceImpl implements FinanceService {

    @Autowired
    private FinanceDao financeDao;


    @Override
    public List<Finance> expenseCarFinanceMsgs(String carcode, Date starttime, Date endtime) {
        List<String> carCodeList = new ArrayList<>();
        carCodeList.add(carcode);
        return  this.expenseFinanceMsgs(carCodeList, starttime, endtime);

    }

    @Override
    public List<Finance> incomeCarFinanceMsgs(String carcode, Date starttime, Date endtime) {
        List<String> carCodeList = new ArrayList<>();
        carCodeList.add(carcode);
        return this.incomeFinanceMsgs(carCodeList, starttime, endtime);
    }



    @Override
    public List<Finance> expenseMonthFinanceMsgs(String deptname, Date starttime, Date endtime) {

        List<Finance> finances = null;
        if(null != deptname){
            List<Cars> carsList = this.findCars(deptname);
            List<String> carCodeList = new ArrayList<>();
            for(Cars car : carsList){
                carCodeList.add(car.getCarcode());
            }
            finances = this.expenseFinanceMsgs(carCodeList, starttime, endtime);
        }

        return finances;
    }

    @Override
    public List<Finance> incomeMonthFinanceMsgs(String deptname, Date starttime, Date endtime) {

        List<Finance> finances = null;
        if(null != deptname){
            List<Cars> carsList = this.findCars(deptname);
            List<String> carCodeList = new ArrayList<>();
            for(Cars car : carsList){
                carCodeList.add(car.getCarcode());
            }
            finances = this.incomeFinanceMsgs(carCodeList, starttime, endtime);
        }

        return finances;
    }



    public List<Finance> expenseFinanceMsgs(List<String> carCodeList , Date starttime, Date endtime) {
        List<Finance> rows = new ArrayList<>();
        if(null != carCodeList && carCodeList.size()>0){

            Finance financeRepair = financeDao.findFinanceRepair(carCodeList, starttime, endtime);
            if(financeRepair.getSumCount()>0){
                financeRepair.setSubName("车辆维护");
                rows.add(financeRepair);
            }

            Finance financeMaintain = financeDao.findFinanceMaintain(carCodeList,starttime,endtime);
            if(financeMaintain.getSumCount()>0){
                financeMaintain.setSubName("车辆保养");
                rows.add(financeMaintain);
            }

            Finance financeInsurance = financeDao.findFinanceInsurance(carCodeList,starttime,endtime);
            if(financeInsurance.getSumCount()>0){
                financeInsurance.setSubName("车辆保险");
                rows.add(financeInsurance);
            }

            Finance financeYearCheck = financeDao.findFinanceYearCheck(carCodeList, starttime, endtime);

            if(financeYearCheck.getSumCount()>0){
                financeYearCheck.setSubName("车辆年检");
                rows.add(financeYearCheck);
            }
        }
        return rows;
    }


    public List<Finance> incomeFinanceMsgs(List<String> carCodeList, Date starttime, Date endtime) {
        List<Finance> rows = new ArrayList<>();
        Finance financeIncome = financeDao.findFinanceIncome(carCodeList, starttime, endtime);
        if(financeIncome.getSumCount()>0){
            financeIncome.setSubName("租车实收");
            rows.add(financeIncome);
        }
        return rows;
    }



    @Override
    public List<Cars> findCars(String deptname) {
        List<Cars> cars = financeDao.findCars(deptname);
        return cars;
    }

    @Override
    public List<Dept> findDepts() {
        List<Dept> depts = financeDao.findDepts();
        return depts;
    }
}
